﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Common.Excel.SmartExcel;
using Enter.Common.statics;
using System.Data;

namespace Enter.Common
{
	public class MyExcel
	{
		private string[] excelTitleName = null;

		public string[] ExcelTitleName
		{
			get { return excelTitleName; }
			set { excelTitleName = value; }
		}

		private string[] dataTitleName = null;

		public string[] DataTitleName
		{
			get { return dataTitleName; }
			set { dataTitleName = value; }
		}

		private string fileNmae = string.Empty;

		public string FileNmae
		{
			get { return fileNmae; }
			set { fileNmae = value; }
		}

		public void DataTableToExcel(DataTable myDataTalbe)
		{
			string[] ExcelName = excelTitleName;
			string[] SqlName = dataTitleName;

			string SaveName = FileNmae;
			FilesStatic.DelFile(FileNmae);
			SmartExcel excel = new SmartExcel();
			excel.CreateFile(SaveName);
			excel.PrintGridLines = false;

			double height = 1.5;

			excel.SetMargin(MarginTypes.TopMargin, height);
			excel.SetMargin(MarginTypes.BottomMargin, height);
			excel.SetMargin(MarginTypes.LeftMargin, height);
			excel.SetMargin(MarginTypes.RightMargin, height);

			string font = "宋体";
			short fontsize = 12;
			excel.SetFont(font, fontsize, FontFormatting.NoFormat);

			byte b1 = 1, b2 = 12;
			short s3 = 18;
			excel.SetColumnWidth(b1, b2, s3);

			string header = "头";
			string footer = "角";
			excel.SetHeader(header);
			excel.SetFooter(footer);

			int cellformat = 0;
			if (myDataTalbe.Rows.Count > 0)
			{
				for (int i = 1, count = ExcelName.Length + 1; i < count; i++)
				{
					excel.WriteValue(ValueTypes.Text, CellFont.Font0, CellAlignment.LeftAlign, CellHiddenLocked.Normal, 1, i, ExcelName[i - 1], cellformat);
				}
				Type ty = null;
				ValueTypes valetype = ValueTypes.Text;
				int num = 2;
				foreach (DataRow myRow in myDataTalbe.Rows)
				{
					for (int x = 1, count = SqlName.Length + 1; x < count; x++)
					{
						ty = myRow[SqlName[x - 1]].GetType();
						if (ty == typeof(int))
						{
							valetype = ValueTypes.Integer;
						}
						else if (ty == typeof(float))
						{
							valetype = ValueTypes.Number;
						}
						else if (ty == typeof(Decimal))
						{
							valetype = ValueTypes.Number;
						}
						else
						{
							valetype = ValueTypes.Text;
						}
						excel.WriteValue(valetype, CellFont.Font0, CellAlignment.LeftAlign, CellHiddenLocked.Normal, num, x, myRow[SqlName[x - 1]], cellformat);
					}
					num++;
				}

			}

			excel.CloseFile();
		}

	}
}
